package com.team.ai.tool;


import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.team.ai.tool.Baidu.Current.*;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import static java.lang.Thread.sleep;

@Component
public class AiTool {
    @Resource
    OCRAuthService ocrAuthService;//OCR令牌
    /**
     * 重要提示代码中所需工具类
     * FileUtil,Base64Util,HttpUtil,GsonUtils请从
     * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
     * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
     * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
     * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
     * 下载
     */
    /**
     * 漫画人脸ai
     *
     * @param filePath 图片路径
     * @return
     */
    public String SelfieAnime(String filePath) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime";
        try {
            // 本地文件路径
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");
            String param = "image=" + imgParam;
            //String accessToken = ocrAuthService.getAuth(5);
            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = ocrAuthService.getAuth(5);
            String result = HttpUtil.post(url, accessToken, param);//发起请求，获取返回结果
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 图片解析 缺图像简介
     *
     * @param filePath 图片文件路径
     * @return
     */
    public String advancedGeneral(String filePath) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general";
        try {
            // 本地文件路径
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");
            String param = "image=" + imgParam;
            String accessToken = ocrAuthService.getAuth(6);
            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 图像清晰度增强
     *
     * @param filePath 图片路径
     * @return
     */
    public String imagedefinitionenhance(String filePath) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/image-process/v1/image_definition_enhance";
        try {
            // 本地文件路径
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");
            String param = "image=" + imgParam;
            String accessToken = ocrAuthService.getAuth(7);
            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 写诗
     *
     * @param text  名称
     * @param index 序号
     * @return
     */

    public String generatePoem(String text, int index) {
        // 请求url
        String url = "https://aip.baidubce.com/rpc/2.0/creation/v1/poem";
        try {
            String param = "{text:" + "'" + text + "'" + ",index:" + index + "}";
            System.out.println(param);
            String accessToken = ocrAuthService.getAuth(8);
            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 春联
     *
     * @param text  名称
     * @param index 序号
     * @return
     */

    public String couplets(String text, int index) {
        // 请求url
        String url = "https://aip.baidubce.com/rpc/2.0/creation/v1/couplets";
        try {
            String param = "{text:" + "'" + text + "'" + ",index:" + index + "}";
            System.out.println(param);
            String accessToken = ocrAuthService.getAuth(12);
            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 祝福语
     *
     * @param text 标题
     * @return
     */
    public String bless_creation(String text) {
        // 请求url
        String url = "https://aip.baidubce.com/rpc/2.0/nlp/v1/bless_creation";
        try {
            String param = "{text:" + "'" + text + "'}";
            System.out.println(param);
            String accessToken = ocrAuthService.getAuth(13);
            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 情感倾向分析
     *
     * @param text
     * @return
     */
    public String Affectivetendencyanalysis(String text) {
        // 请求url
        String url = "https://aip.baidubce.com/rpc/2.0/nlp/v1/sentiment_classify";
        try {
            String param = "{text:" + "'" + text + "'}";
            System.out.println(param);
            String accessToken = ocrAuthService.getAuth(9);
            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 地址识别
     *
     * @param text
     * @return
     */
    public String address(String text) {
        // 请求url
        String url = "https://aip.baidubce.com/rpc/2.0/nlp/v1/address";
        try {
            String param = "{text:" + "'" + text + "'}";
            System.out.println(param);
            String accessToken = ocrAuthService.getAuth(10);
            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 文本纠错
     *
     * @param text
     * @return
     */
    public String ecnet(String text) {
        // 请求url
        String url = "https://aip.baidubce.com/rpc/2.0/nlp/v1/ecnet";
        try {
            String param = "{text:" + "'" + text + "'}";
            System.out.println(param);
            String accessToken = ocrAuthService.getAuth(11);
            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 黑白变色
     */

    public String colourize(String filePath) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/image-process/v1/colourize";
        try {
            // 本地文件路径
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");
            String param = "image=" + imgParam;
            String accessToken = ocrAuthService.getAuth(14);
            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 通用文字识别
     */

    public String generalBasic(String filePath) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic";
        try {
            // 本地文件路径
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");
            String param = "image=" + imgParam;
            String accessToken = ocrAuthService.getAuth(16);
            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 油画
     *
     * @param text       输入内容
     * @param resolution 图片大小
     * @param style      风格
     * @param num        数量
     * @return
     */
    public String txt2img(String text, String resolution, String style, int num) {
        // 请求url
        String url = "https://aip.baidubce.com/rpc/2.0/ernievilg/v1/txt2img";
        Map<String,Object> map = new HashMap<>();
        map.put("text",text);
        map.put("style",style);
        map.put("resolution",resolution);
        map.put("num",num);
        String param = GsonUtils.toJson(map);
        String accessToken = ocrAuthService.getAuth(15);
        String result = null;
        try {
            result = HttpUtil.post(url, accessToken, param);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println(result);
        String taskId = new JSONObject(result).getJSONObject("data").getStr("taskId");//            System.out.println(taskId.toString());
        return taskId;
    }

    /**
     * 油画输出
     * @param taskId
     * @return
     */
    public String getImg(String taskId) {
        // 请求url
        String url = "https://aip.baidubce.com/rpc/2.0/ernievilg/v1/getImg";
        Map<String,Object> map1 = new HashMap<>();
        map1.put("taskId",taskId);
        String param = GsonUtils.toJson(map1);
        String accessToken = ocrAuthService.getAuth(15);
        String result = null;
        try {
            result = HttpUtil.post(url, accessToken, param);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println(result);
        return result;
    }

    /**
     * 植物识别
     * @param filePath
     * @return
     */
    public String plant(String filePath) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/plant";
        try {
            // 本地文件路径
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");
            String param = "image=" + imgParam;
            String accessToken = ocrAuthService.getAuth(19);
            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 果蔬识别
     * @param filePath
     * @return
     */
    public String ingredient(String filePath) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/classify/ingredient";
        try {
            // 本地文件路径
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");
            String param = "image=" + imgParam;
            String accessToken = ocrAuthService.getAuth(20);
            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 动物识别
     * @param filePath
     * @return
     */
    public String animal(String filePath) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/animal";
        try {
            // 本地文件路径
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");
            String param = "image=" + imgParam;
            String accessToken = ocrAuthService.getAuth(2);
            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }







    /**
     * 青云
     *
     * @param content 输入的问题
     * @return
     */
    public String aiReply(String content) {
        String body = HttpRequest.get("http://api.qingyunke.com/api.php?key=free&appid=0&msg=" + URLUtil.encode(content))
                .execute().charset("utf-8").body();
        JSONObject jsonObject = JSONUtil.parseObj(body);
        if (jsonObject.getStr("result").equals("0")) {
            return jsonObject.getStr("content");
        } else {
            return "操作频繁！！！";
        }
    }
}
